package com.seednia.module.core.xport;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seednia.foundation.framework.service.AbstractService;
import com.seednia.foundation.framework.service.ServiceException;

/**
 * 
 * @author Young Suk Ahn
 *
 */
public class XportDefaultService extends AbstractService implements IXportService {

	private Map<String, IExporter> exporters = Collections.synchronizedMap( new HashMap<String, IExporter>() );
	private Map<String, IImporter> importers = Collections.synchronizedMap( new HashMap<String, IImporter>() );
	
	private static final Log logger = LogFactory.getLog(XportDefaultService.class);
	
	@Override
	public void initialize() {
		exporters.put(ExporterCsv.FORMAT, new ExporterCsv());
		exporters.put(ExporterXml.FORMAT, new ExporterXml());
		
		importers.put(ImporterCsv.FORMAT, new ImporterCsv());
		importers.put(ImporterXml.FORMAT, new ImporterXml());
	}
	
	public Map<String, IExporter> getExporters() {
		return exporters;
	}

	public void setExporters(Map<String, IExporter> exporters) {
		this.exporters = exporters;
	}

	public IExporter getExporter(String format)
	{
		return exporters.get(format);
	}
	
	public Map<String, IImporter> getImporters() {
		return importers;
	}

	public void setImporters(Map<String, IImporter> importers) {
		this.importers = importers;
	}

	public IImporter getImporter(String format)
	{
		return importers.get(format);
	}

	@Override
	public void exportAppData(String format, String comments, OutputStream os, Map<String, Object> params) throws ServiceException 
	{
		IExporter xportProvider = getExporter(format);
		try {
			xportProvider.exportAppData(comments, os, params);
		} catch (XportException e) {
			logger.error("XportException at XportDefaultService.exportAppData", e );
			throw new ServiceException(e);
		}
	}

	@Override
	public void exportAppData(String format, String comments, File file, Map<String, Object> params) throws ServiceException {
		
		IExporter xportProvider = getExporter(format);
		try {
			xportProvider.exportAppData(comments, file, params);
			
		} catch (FileNotFoundException e) {
			logger.error("FileNotFoundException at XportDefaultService.exportAppData", e );
			throw new ServiceException(e);
		} catch (IOException e) {
			logger.error("IOException at XportDefaultService.exportAppData", e );
			throw new ServiceException(e);
		} catch (XportException e) {
			logger.error("XportException at XportDefaultService.exportAppData", e );
			throw new ServiceException(e);
		}
		
	}

	@Override
	public void importAppData(String format, InputStream xmlInput, Map<String, Object> params)  throws ServiceException {
		
		IImporter importer = getImporter(format);
		
		try {
			importer.importAppData(xmlInput, params);
		} catch (XportException e) {
			logger.error("XportException at XportDefaultService.importAppData", e );
			throw new ServiceException(e);
		}
		
	}

	@Override
	public void importAppData(String format, File file, Map<String, Object> params) throws ServiceException {
		IImporter importer = getImporter(format);
		
		try {
			importer.importAppData(file, null);
		} catch (XportException e) {
			logger.error("XportException at XportDefaultService.importAppData", e );
			throw new ServiceException(e);
		} catch (FileNotFoundException e) {
			logger.error("FileNotFoundException at XportDefaultService.importAppData", e );
			throw new ServiceException(e);
		} catch (IOException e) {
			logger.error("IOException at XportDefaultService.importAppData", e );
			throw new ServiceException(e);
		}
	}

}
